package org.anyin.gitee.shiro.config;

import org.anyin.gitee.shiro.advisor.ApiMessageAdvisor;
import org.anyin.gitee.shiro.base.MdcExecutor;
import org.anyin.gitee.shiro.extension.DefaultExtensionHandlerFactoryImpl;
import org.anyin.gitee.shiro.extension.IExtensionHandlerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class AppConfig {

    @Bean
    public ApiMessageAdvisor apiMessageAdvisor(){
        return new ApiMessageAdvisor();
    }

    @Bean
    public IExtensionHandlerFactory extensionHandlerFactory(){
        return new DefaultExtensionHandlerFactoryImpl();
    }

    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(200);
        executor.setThreadNamePrefix("AsyncExecutorThread-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        // 处理request-id打印的问题
        return new MdcExecutor(executor);
    }
}
